package com.tgy.bean;

import com.tgy.interceptor.Interceptor;
import com.tgy.interceptor.Invocation;
import org.springframework.stereotype.Component;

@Component
public class TransactionInterceptor implements Interceptor {
  @Override
  public Object invoke(Invocation invocation) throws Throwable {

    try {
      //开启事务
      System.out.println("开启事务");
      Object proceed = invocation.proceed();

      System.out.println("提交事务");
      return proceed;
    }catch (Exception e){
      e.printStackTrace();
      System.out.println("事务回滚");
      return null;
    }finally {

      //关闭事务
      System.out.println("关闭事务");
    }

  }
}
